package com.andryyu.learn.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatTextView
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andryu.base.LogUtils
import com.andryu.base.adapter.LearnListAdapter
import com.andryu.base.entity.LearnVideoPageEntity
import com.andryu.learn.R

class LearnVideoAdapter(private val categoryList:MutableList<LearnVideoPageEntity<*>>) :
    RecyclerView.Adapter<LearnVideoAdapter.ViewHolder>(){

    private var mListener: ((entity: LearnVideoPageEntity<*>) -> Unit?)? = null
    private val mBackgroundColors = listOf(
        com.andryu.ui.R.color.light_pink,
        com.andryu.ui.R.color.light_blue,
        com.andryu.ui.R.color.light_green,
        com.andryu.ui.R.color.light_yellow
    )

    fun setOnItemClick(listener:(entity: LearnVideoPageEntity<*>) -> Unit){
        mListener = listener
    }

    fun updateItems(mutableList: MutableList<LearnVideoPageEntity<*>>){
        categoryList.clear()
        categoryList.addAll(mutableList)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_page_video, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = categoryList.size

    @SuppressLint("ResourceAsColor")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val entity = categoryList[position]
        val index = position % mBackgroundColors.size
        if (holder.itemView is CardView){
            (holder.itemView as CardView).setCardBackgroundColor(ContextCompat.getColor(holder.itemView.context, mBackgroundColors[index]))
        }
        holder.bind(entity)
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val mItemName: AppCompatTextView = itemView.findViewById(R.id.tv_item_header)
        private val mRvSubVideo:RecyclerView = itemView.findViewById(R.id.rv_sub_video)

        @SuppressLint("ClickableViewAccessibility")
        fun bind(data: LearnVideoPageEntity<*>){
            mItemName.text = data.name
            mRvSubVideo.layoutManager = LinearLayoutManager(itemView.context)
            val mAdapter = LearnListAdapter(data.dataList)
            mRvSubVideo.adapter = mAdapter
            mAdapter.setOnItemClick {
                data.jumpId = it.intentId
                mListener?.invoke(data)
            }
            itemView.setOnClickListener {
                mListener?.invoke(data)
            }

            mRvSubVideo.setOnTouchListener { _, event ->
                if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_UP) {
                    mRvSubVideo.parent.requestDisallowInterceptTouchEvent(true)
                }
                false
            }
        }
    }
}